import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../../components/newclassify.dart';
import '../../components/newgoods.dart';
import '../../components/newhome.dart';
import '../../components/newmy.dart';

class Newhome extends StatefulWidget {
  const Newhome({super.key});

  @override
  _NewhomeState createState() => _NewhomeState();
}

class _NewhomeState extends State<Newhome> {
  List tabList = [
    {
      'label': '首页',
      'activeIcon': 'assets/tabbar/home-active.png',
      'icon': 'assets/tabbar/home.png',
    },
    {
      'label': '分类',
      'activeIcon': 'assets/tabbar/cate-active.png',
      'icon': 'assets/tabbar/cate.png',
    },
    {
      'label': '购物车',
      'activeIcon': 'assets/tabbar/cart-active.png',
      'icon': 'assets/tabbar/cart.png',
    },
    {
      'label': '我的',
      'activeIcon': 'assets/tabbar/user-active.png',
      'icon': 'assets/tabbar/user.png',
    }
  ];
  int currentIndex = 0;

  List<BottomNavigationBarItem> getDataList() {
    List<BottomNavigationBarItem> list = [];
    for (var item in tabList) {
      list.add(
        BottomNavigationBarItem(
            icon: Image.asset(
              item['icon'],
              width: 24,
              height: 24,
            ),
            activeIcon: Image.asset(
              item['activeIcon'],
              width: 24,
              height: 24,
            ),
            label: item['label']),
      );
    }
    return list;
  }

  Map list = {};
  List bannerlist = [];
  List adlist = [];
  @override
  void initState() {
    super.initState();
    getResDate();
  }

  void getResDate() async {
    Dio dio = Dio();
    final res = await dio
        .get('http://smart-shop.itheima.net/index.php?s=/api/page/detail');
    list = res.data['data']['pageData'];
    setState(() {
      bannerlist = list['items'][3]['data'];
      adlist = list['items'][4]['data'];
    });
    print(adlist);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: const Color.fromRGBO(245, 67, 54, 1),
        title: const Text('智慧商城'),
        foregroundColor: Colors.white,
      ),
      backgroundColor: Colors.blue,
      body: IndexedStack(
        index: currentIndex,
        children: [
          Newshome(list: bannerlist),
          const Newclassify(),
          const Newgoods(),
          const Newmy()
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        selectedItemColor: const Color.fromRGBO(245, 67, 54, 1),
        // 未选中颜色
        unselectedItemColor: Colors.black,
        items: getDataList(),
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }
}
